package com.chendongyang.www.dao; //进行用户账号的注册和登录;
import com.chendongyang.www.po.bileM;
import com.chendongyang.www.po.chargingStation;
import com.chendongyang.www.po.user;
import com.mysql.cj.jdbc.Driver;

import java.sql.*;
import java.util.ArrayList;

public class lodgings{
     public  int insert(user user,String identity,String name) throws Exception{
          //进行数据的插入（用户注册）
          DriverManager.registerDriver(new Driver());
        Connection root = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lle", "root", "123456");
        String sql="insert into dianche1 values (?,?,?,?,0,null,null,null);";
        int line;
          PreparedStatement preparedStatement = root.prepareStatement(sql);
          preparedStatement.setString(1, user.getId());
              preparedStatement.setString(2, user.getPassword());
              preparedStatement.setString(3,identity);
              preparedStatement.setString(4,name);
              line = preparedStatement.executeUpdate();
              preparedStatement.close();
          root.close();
        return line;
    }
    public boolean select1(String userName) throws SQLException {
          //判断要注册时该注册名是否已存在；
        DriverManager.registerDriver(new Driver());
        System.out.println(userName);
        Connection root = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lle", "root", "123456");
        String sql="select id from dianche1 where id=?;";
        PreparedStatement preparedStatement = root.prepareStatement(sql);
        preparedStatement.setString(1,userName);
        ResultSet resultSet = preparedStatement.executeQuery();
        boolean result=true;
        while(resultSet.next()){
            result=false;
        }
        root.close();
        preparedStatement.close();
        resultSet.close();
        return result;
    }
      public boolean select(user user) throws SQLException {
          //进行登录时的数据检查，判断是否符合条件；
          DriverManager.registerDriver(new Driver());
          Connection root = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lle", "root", "123456");
          String sql = "select * from dianche1 where id=? and password=?;";
          PreparedStatement preparedStatement = root.prepareStatement(sql);
          preparedStatement.setString(1, user.getId());
          preparedStatement.setString(2, user.getPassword());
          ResultSet resultSet = preparedStatement.executeQuery();
          boolean result = resultSet.next();
          root.close();
          preparedStatement.close();
          resultSet.close();
          return result;
      }
      public int bileInsert(bileM bileM,String kehu) throws SQLException {
         //完善电动车的信息；
          DriverManager.registerDriver(new Driver());
          Connection root = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lle", "root", "123456");
          String sql="update  dianche1 set bileName=?,bileAge=?,bileBrand=? where id=?;";
          int line;
          PreparedStatement preparedStatement = root.prepareStatement(sql);
          preparedStatement.setString(1, bileM.getBileName());
          preparedStatement.setInt(2, bileM.getBileAge());
          preparedStatement.setString(3, bileM.getBileBrand());
          preparedStatement.setString(4, kehu);
          line = preparedStatement.executeUpdate();
          preparedStatement.close();
          root.close();
          return line;
      }
    public ArrayList<chargingStation> select(String Id) throws SQLException {
        //查询可用于充电的充电口
       // String id=Id;
        DriverManager.registerDriver(new Driver());
        Connection root=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lle","root","123456");
        String sql="select * from  charging where status =?;";
        PreparedStatement preparedStatement = root.prepareStatement(sql);
        //Statement statement=root.createStatement();
        ResultSet resultSet=preparedStatement.executeQuery(sql);
        //System.out.println(resultSet.getString(1));
          preparedStatement.setString(1,Id);
        //System.out.println(List.get(1));
        ArrayList<chargingStation> list=new ArrayList<>();
        while(resultSet.next()){
            chargingStation user=new chargingStation();
            user.setChargeS(resultSet.getString(1));
            user.setChargeB(resultSet.getString(2));
            list.add(user);
        }
        root.close();
        preparedStatement.close();
        resultSet.close();
        return list;
    }
}

